package cn.tedu.tmall.admin.mall.service.impl;

import cn.tedu.tmall.admin.mall.dao.repository.IMallCategoryRepository;
import cn.tedu.tmall.admin.mall.dao.repository.IMallGoodsRepository;
import cn.tedu.tmall.admin.mall.pojo.param.MallGoodsAddParam;
import cn.tedu.tmall.admin.mall.pojo.po.MallGoodsDetailPO;
import cn.tedu.tmall.admin.mall.pojo.po.MallGoodsPO;
import cn.tedu.tmall.admin.mall.pojo.vo.MallGoodsDetailVO;
import cn.tedu.tmall.admin.mall.pojo.vo.MallGoodsVO;
import cn.tedu.tmall.admin.mall.service.IMallGoodsService;
import cn.tedu.tmall.common.exception.ServiceException;
import cn.tedu.tmall.common.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务层-商城商品接口实现类
 */
@Slf4j
@Service
public class MallGoodsService implements IMallGoodsService {
    @Autowired
    private IMallGoodsRepository mallGoodsRepository;
    @Autowired
    private IMallCategoryRepository mallCategoryRepository;

    /**
     * 新增商品
     * 业务逻辑：校验分类，校验名称，然后新增；保证事务全部成功
     */
    @Override
    public void addGoods(MallGoodsAddParam goodsAddParam) {
        log.debug("Service-新增商品-入参:{}",goodsAddParam);
        //校验分类
        Long categoryId = goodsAddParam.getCategoryId();
        log.debug("Service-新增商品-分类id:{}",categoryId);
        int countCategory = mallCategoryRepository.countCategoryById(categoryId);
        if (countCategory == 0){
            log.error("Service-新增商品-分类不存在:{}",categoryId);
            throw new ServiceException(ServiceCode.FAIL.getCode(), "分类不存在！");
        }
        //校验商品标题
        String title = goodsAddParam.getTitle();
        int countTitle = mallGoodsRepository.countGoodsByTitle(title);
        if (countTitle == 1){
            log.error("Service-新增商品-标题已存在:{}",title);
            throw new ServiceException(ServiceCode.FAIL.getCode(), "商品标题已存在！");
        }
        //通过校验，新增商品
        MallGoodsPO mallGoodsPO = new MallGoodsPO();
        BeanUtils.copyProperties(goodsAddParam,mallGoodsPO);
        log.debug("Service-新增商品-新增商品基本信息:{}",mallGoodsPO);
        int addGoods = mallGoodsRepository.saveGoods(mallGoodsPO);
        if (addGoods == 0){
            log.error("Service-新增商品-新增商品失败");
            throw new ServiceException(ServiceCode.FAIL.getCode(), "服务端故障，新增商品失败！");
        }
        //新增商品详情
        MallGoodsDetailPO mallGoodsDetailPO = new MallGoodsDetailPO();
        mallGoodsDetailPO.setGoodsId(mallGoodsPO.getId());
        mallGoodsDetailPO.setDetail(goodsAddParam.getDetail());
        log.debug("Service-新增商品-新增商品详情:{}",mallGoodsDetailPO);
        int addGoodsDetail = mallGoodsRepository.saveGoodsDetail(mallGoodsDetailPO);
        if (addGoodsDetail == 0){
            log.error("Service-新增商品-新增商品失败");
            throw new ServiceException(ServiceCode.FAIL.getCode(), "服务端故障，新增商品失败！");
        }
    }

    /**
     * 根据分类id获取商品列表
     */
    @Override
    public List<MallGoodsVO> listGoods(Long categoryId) {
        log.debug("Service-根据分类id获取商品列表-入参:{}",categoryId);
        //校验分类id
        int countCategory = mallCategoryRepository.countCategoryById(categoryId);
        if (countCategory == 0){
            log.error("Service-根据分类id获取商品列表-分类不存在:{}",categoryId);
            throw new ServiceException(ServiceCode.FAIL.getCode(),"分类不存在！");
        }
        List<MallGoodsPO> goodsPOList = mallGoodsRepository.listGoods(categoryId);
        log.debug("Service-根据分类id获取商品列表-商品列表:{}",goodsPOList);
        return mallGoodsPOS2VOS(goodsPOList);
    }

    @Override
    public MallGoodsDetailVO detail(Long goodsId) {
        log.debug("Service-根据商品id获取商品详情-入参:{}",goodsId);
        //校验商品id
        int countGoods = mallGoodsRepository.countGoodsById(goodsId);
        if (countGoods == 0){
            log.error("Service-根据商品id获取商品详情-商品不存在:{}",goodsId);
            throw new ServiceException(ServiceCode.FAIL.getCode(),"商品不存在！");
        }
        //获取商品基本信息和详情
        MallGoodsPO goodsBasic = mallGoodsRepository.getGoodsBasicById(goodsId);
        MallGoodsDetailPO goodsDetailPO = mallGoodsRepository.getGoodsDetailByGoodsId(goodsId);
        //封装商品详情
        MallGoodsDetailVO goodsDetailVO = new MallGoodsDetailVO();
        BeanUtils.copyProperties(goodsBasic,goodsDetailVO);
        BeanUtils.copyProperties(goodsDetailPO,goodsDetailVO);
        log.debug("Service-根据商品id获取商品详情-商品详情:{}",goodsDetailVO);
        return goodsDetailVO;
    }

    private MallGoodsVO mallGoodsPO2VO(MallGoodsPO mallGoodsPO){
        MallGoodsVO mallGoodsVO = new MallGoodsVO();
        BeanUtils.copyProperties(mallGoodsPO,mallGoodsVO);
        return mallGoodsVO;
    }

    private List<MallGoodsVO> mallGoodsPOS2VOS(List<MallGoodsPO> goodsPOList){
        List<MallGoodsVO> vos = new ArrayList<>();
        for (MallGoodsPO po : goodsPOList) {
            vos.add(mallGoodsPO2VO(po));
        }
        return vos;
    }
}
